package com.ophiux.cs.application.consultOrder.impl;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ophiux.cs.application.consultOrder.dto.DoctorOrderCSRequest;
import com.ophiux.cs.application.consultOrder.dto.DoctorOrderCSResponse;
import com.ophiux.cs.application.consultOrder.service.IDoctorOrderCSAppService;
import com.ophiux.cs.application.core.Result;
import com.ophiux.cs.application.core.ResultEnum;
import com.ophiux.cs.domain.consultOrder.model.entity.CSOnlineRecords;
import com.ophiux.cs.domain.consultOrder.repositories.ICSOnlineRecordRepository;
import com.ophiux.utils.common.ShiftUtils;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class DoctorOrderCSAppService implements IDoctorOrderCSAppService {
	@Autowired
	ICSOnlineRecordRepository csOnlineRecordRepository;
	@Override
	public Result<DoctorOrderCSResponse> DoctorOrderCS(DoctorOrderCSRequest dto) {
//		DoctorOrderCSResponse resp = new DoctorOrderCSResponse();
		try {
			/**
			 * 1.验证参数是否非法
			 * 2.验证咨询记录是否存在
			 * 3.接单、拒单
			 */
			if(!dto.isParamValid()) {
				return Result.fail(ResultEnum.requestParamFail);
			}
			
			CSOnlineRecords obj = csOnlineRecordRepository.getRecordById(dto.getRECORD_ID());
			if(obj==null) {
				return Result.fail(ResultEnum.NotFoundConsultRecord);
			}
			
			
			obj = (CSOnlineRecords) ShiftUtils.objToNewObj(dto, CSOnlineRecords.class);
			if(DoctorOrderCSRequest.STATUS_0.equals(dto.getSTATUS())){
				obj.refuseOrder();
			}else if(DoctorOrderCSRequest.STATUS_1.equals(dto.getSTATUS())) {
				obj.acceptOrder();
			}
			csOnlineRecordRepository.update(obj);
			
		} catch (Exception e) {
			log.info("程序异常：",e);
			e.printStackTrace();
			return Result.fail(ResultEnum.requestException);
		}
		return Result.success();
	}

}
